﻿#ifndef PST_GRAPH2DWINDOWBASE_H
#define PST_GRAPH2DWINDOWBASE_H

#include <QWidget>
#include <QList>
#include <QHash>
#include "InfomationCurve2DBasical.h"
#include "PostEnumType.h"
#include "parser/JsonDef.h"
class QCPLayerable;
class QStandardItem;
class QCPGraph;
class QDomElement;
class QDomDocument;
class QDomNodeList;

namespace pst
{
    class Curve2DPlotor;

    /// @brief 二维图形
    class Graph2DWindowBase : public QWidget
    {
        Q_OBJECT
    public:
        explicit Graph2DWindowBase(QWidget* parent = 0);
        virtual ~Graph2DWindowBase();

        virtual void addGraph2D(Info2DPointer& info); // 传引用，改数据

        bool saveScreenShot(const QString& path);

        virtual void setWindowID(int windowID) = 0;
        virtual int getWindowID() const = 0;

        virtual void setWindowUniversalID(int windowUniversalID) = 0;
        virtual int getWindowUniversalID() const = 0;

        //inline QList<Info2DPointer>& getGraph2DInfoList() { return m_graph2DInfoList; }

        inline Curve2DPlotor* getPlotor() const { return m_customPlot; }
        inline const QList<QPair<QStandardItem*, Info2DPointer>>& getCurve() const { return m_curveMap; }
        void addCurveItemInfoPair(QStandardItem* item, Info2DPointer& info);
        Info2DPointer getCurveInfoByItem(QStandardItem* item) const;
        QStandardItem* getItemFromCurveInfo(Info2DPointer& info) const;
        bool judgmentItemInfoPairIsExist(QStandardItem* item) const;
        void removeCurveItemInfoPair(QStandardItem* item);

        Info2DPointer getInfoFromItem(QStandardItem* item) const; 

        /// @brief 文件写出至工程文件
        virtual QDomElement& writeToProjectFile(QDomDocument* doc, QDomElement* element, bool isdiso = false) = 0;

        /// @brief 从工程文件读入数据
        virtual void readFromProjectFile(QDomElement* e, bool isdiso = false) = 0;

        Curve2DPlotor* getCustomPlot()
        {
            return m_customPlot;
        }

    signals:
        void signal_generateANewGraphCurve(QList<Info2DPointer>& infos);
        void signal_getTwoDResultDataPointer(const TextFileParser* textFIleParser, ResultDataPointer& resultDataPointer);
        void signal_updateMeasureButtonState(int state);

    public slots:
        virtual void slot_adaptiveRange();
        virtual void slot_amplify();
        virtual void slot_minification();

        virtual void slot_chosenItemsChanged(const QList<QStandardItem*>& itemList);

        virtual void slot_generateANewGraphCurve(const QList<QStandardItem*>& itemList,
                                                 const Info2DPointer& changedInfo);

        void slot_updateCurveData(const QList<int> selectedCurveInfoIDList,
                                  const int changedInfoID, const QVector<int>& otherKeyIndex = QVector<int>());

        void slot_legendTitleFontFamilyChanged(const QString& fontFamily);
        void slot_legendTitleFontSizeChanged(int fontSize);

    protected:
        virtual void initConnections();
        virtual void updateCurve(QStandardItem* item, Info2DPointer& info);

        void normalizeAndLog(QVector<double>& vec, bool isNor, bool isLog, bool isPower = false);

        //QList<Info2DPointer> m_graph2DInfoList;

        Curve2DPlotor* m_customPlot{nullptr};
        QList<QCPLayerable*> m_graphs; /// QCPGraph
        // 存放设置曲线列表中的曲线和对应的曲线信息，用于从设置栏曲线中重新找回曲线信息
        QList<QPair<QStandardItem*, Info2DPointer>> m_curveMap;
        QList<QColor> m_colorList;

        QList<Info2DPointer> m_addCurveInfo;

    private:
        void InitializeResultWindowStyle();
    };
} // namespace pst

#endif // PST_GRAPH2DWINDOWBASE_H
